﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;

public partial class ChuDe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Params["IdCd"] != null)
            {
                H_SubTin.Value = Request.Params["IdCd"].ToString();
                View_TinTuc(Convert.ToInt32(H_SubTin.Value));

            }
            HienThiSubTin();
            Pager1.CurrentIndex = 1;
            HienThiSubTinall(Pager1.CurrentIndex);
        }

    }

    protected void View_TinTuc(int IdSubTin)
    {
        DmSubTinTuc entTinTuc = DataRepository.DmSubTinTucProvider.GetById(IdSubTin);
        try
        {
            //if (entTinTuc != null)
            //{
            DmLoaiTinTuc entnhomtin = DataRepository.DmLoaiTinTucProvider.GetById(entTinTuc.IdNhomTinTuc);
            DmSubTinTuc entSubTin = DataRepository.DmSubTinTucProvider.GetById(entTinTuc.Id);
            Session["Title"] = entnhomtin.Ten + " >> " + entSubTin.TenSubTinTuc;
            litTieude.Text = entTinTuc.TenSubTinTuc;
            //DmLoaiTinTuc entLoaiTin = DataRepository.DmLoaiTinTucProvider.GetById(entTinTucId.IdLoaiTinTuc);
            Hyperlink2.NavigateUrl = "~/" + GetTieuDe(entnhomtin.Ten) + ".aspx";
            Hyperlink3.NavigateUrl = "~/chude/" + entTinTuc.Id + "_" + entTinTuc.TenSubTinTuc + ".aspx";

            //H_SubTin.Value = (entTinTucId.IdSubTinTuc).ToString();
            int TotalRows = 0;
            DataRepository.DmSubTinTucProvider.GetPaged("IdNhomTinTuc=" + entSubTin.IdNhomTinTuc, "", 0, 0, out TotalRows);
            if (TotalRows > 1)
            {
                panelnhom.Visible = true;
                panelnhom2.Visible = false;
                lblNhomTintop.Text = entnhomtin.Ten;
            }
            else
            {
                panelnhom.Visible = false;
                panelnhom2.Visible = true;
                lblNhomTintop.Text = "";
                lblsubnhom2.Text = entSubTin.TenSubTinTuc;
            }
            lblSubNhomTin.Text = entTinTuc.TenSubTinTuc;
            //}
        }
        catch (Exception ex)
        { }
    }

    /// <summary>
    /// Hiển Thị Sub Tin
    /// </summary>
    private void HienThiSubTin()
    {
        int TotalRows = 0;
        TList<TinTuc> TList_TinTuc = DataRepository.TinTucProvider.GetPaged("IdSubTinTuc=" + Convert.ToInt32(H_SubTin.Value), "NgayCapNhat DESC", 0, 2, out TotalRows);
        dlChuDe.DataSource = TList_TinTuc;
        dlChuDe.DataBind();
    }

    /// <summary>
    /// Hiển Thị Sub Tin
    /// </summary>
    protected void HienThiSubTinall(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "IdSubTinTuc=" + Convert.ToInt32(H_SubTin.Value);

        TList<TinTuc> TList_TinTuc = DataRepository.TinTucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
        dlChudeall.DataSource = TList_TinTuc;
        dlChudeall.DataBind();

        Pager1.ItemCount = TotalRows;
        H_PageCount.Value = Pager1.PageCount.ToString();
    }

    /// <summary>
    /// Handles ItemDataBound, Binds ExchangesTo to the Child DataList
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dlFromTrades_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            DataListItem parentList = e.Item;
            HiddenField lit_Name = (HiddenField)parentList.FindControl("H_CatalogId");
            string i = (lit_Name.Value).ToString();
            int j = Convert.ToInt32(i);            
        }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            HienThiSubTinall(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("ChuDe.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdNext_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex < Convert.ToInt32(H_PageCount.Value))
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex + 1;
            HienThiSubTinall(Pager1.CurrentIndex);
        }
    }

    protected void cmdPrevious_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex > 1)
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex - 1;
            HienThiSubTinall(Pager1.CurrentIndex);
        }
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    public string GetLienKet()
    {
        return Common.sPathUpLienKet;
    }

    public string GetNgay(object ngayviet)
    {
        DateTime s = Convert.ToDateTime(ngayviet);
        string v = s.ToString("HH:mm | dd/MM/yyyy");
        return Common.StandardStringjquery(v);
    }
}